package com.chc.web.user_repair.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chc.utils.Msg;
import com.chc.web.user_repair.entity.UserRepair;
import com.chc.web.user_repair.entity.UserRepairParm;
import com.chc.web.user_repair.service.UserRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/01/21/10:00
 * @Description: 维修管理模块
 */
@RestController
@RequestMapping("/api/userRepair")
public class UserRepairController {

    @Autowired
    private UserRepairService userRepairService;

    /**
     * 我的报修列表
     */
    @GetMapping("/myList")
    public Msg getMyList(UserRepairParm userRepairParm){
        //构造条件
        QueryWrapper<UserRepair> query = new QueryWrapper();
        query.lambda().eq(UserRepair::getUserId,userRepairParm.getUserId())
                .like(UserRepair::getRepairContent,userRepairParm.getRepairContent())
                .orderByDesc(UserRepair::getCommitTime);
        //构造分页对象
        IPage<UserRepair> page = new Page<>();
        page.setCurrent(userRepairParm.getCurrentPage());
        page.setSize(userRepairParm.getPageSize());
        IPage<UserRepair> list = userRepairService.page(page, query);
        return Msg.success("查询成功",list);
    }


    /**
     * 报修列表(物业管理部人员)
     */
    @GetMapping("/list")
    public Msg getList(UserRepairParm userRepairParm){
        //构造条件
        QueryWrapper<UserRepair> query = new QueryWrapper();
        query.lambda().like(UserRepair::getRepairContent,userRepairParm.getRepairContent())
                .orderByDesc(UserRepair::getCommitTime);
        //构造分页对象
        IPage<UserRepair> page = new Page<>();
        page.setCurrent(userRepairParm.getCurrentPage());
        page.setSize(userRepairParm.getPageSize());
        IPage<UserRepair> list = userRepairService.page(page, query);
        return Msg.success("查询成功",list);
    }

    /**
     * 新增
     * */
    @PostMapping
    @PreAuthorize("hasAuthority('sys:myRepair:add')")
    public Msg add(@RequestBody UserRepair userRepair){
        //设置时间
        userRepair.setCommitTime(new Date());
        //设置处理状态
        userRepair.setStatus("0");
        boolean b = userRepairService.save(userRepair);
        if(b){
            return Msg.success("报修成功!");
        }
        return Msg.error("报修失败!");
    }

    /**
     * 编辑
     * */
    @PutMapping
    @PreAuthorize("hasAnyAuthority('sys:myRepair:edit','sys:repairList:do')")
    public Msg edit(@RequestBody UserRepair userRepair){
        boolean b = userRepairService.updateById(userRepair);
        if(b){
            return Msg.success("编辑成功!");
        }
        return Msg.error("编辑失败!");
    }

    /**
     * 删除
     * */
    @DeleteMapping("/{repairId}")
    @PreAuthorize("hasAuthority('sys:myRepair:delete')")
    public Msg delete(@PathVariable("repairId") Long repairId){
        boolean b = userRepairService.removeById(repairId);
        if(b){
            return Msg.success("删除成功!");
        }
        return Msg.error("删除失败!");
    }
}
